if exists (select 1
          from sysobjects
          where  id = object_id('bd_fn_datetimeToInt')
          and type in ('IF', 'FN', 'TF'))
   drop function bd_fn_datetimeToInt
go


create function bd_fn_datetimeToInt (@curDate DATETIME,@cardDate DATETIME)
RETURNS INT
BEGIN
  DECLARE @result INT, @day INT = 0

  SET @day = DATEDIFF(DAY, @curDate, @cardDate)

  IF(@day < 0)
  BEGIN
  	SET @result = (ABS(@day) - 1) * 1440 + DATEPART(HOUR,@cardDate) * 60 + DATEPART(MINUTE,@cardDate)
    SET @result = -@result
  END ELSE
  BEGIN
  	SET @result = @day * 1440 + DATEPART(HOUR,@cardDate) * 60 + DATEPART(MINUTE,@cardDate)
  END

  RETURN @result
END
go
